//
//  AllProjectRequest.m
//  UserUse
//
//  Created by angra on 16/10/14.
//  Copyright © 2016年 CJPC. All rights reserved.
//

#import "AllProjectRequest.h"
#import "LLHttpRequest.h"
#import "LLOrderRequestModel.h"

@implementation AllProjectRequest
//登录
+(void)loginWithUserPhone:(NSString *)userphone userpwd:(NSString *)userpwd userType:(NSString *)usertype success:(void(^)(id responseObject))success failure:(void(^)(NSString *error))failure
{
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    hub.labelText = MBProgressHUDModeIndeterminate;
   
    hub.labelText = @"登录中...";
    NSDictionary *paramsDic = @{
                                @"userphone":userphone,
                                @"userpwd":userpwd,
                                @"usertype":@"0",
                                @"token":cookie_token
                                };
    [LLHttpRequest post:@"index/login.do" parameters:paramsDic success:^(id responseObject) {
        NSLog(@"%@--->登录",responseObject);
        success(responseObject);
        hub.mode = MBProgressHUDModeText;
        
        hub.labelText = responseObject[@"resultMassage"];
        [hub showAnimated:YES whileExecutingBlock:^{
            sleep(1);
        }completionBlock:^{
            [hub removeFromSuperview];
        }];
    } failure:^(NSError *error) {
        hub.hidden = YES;
    }];
}
//获取token
+(void)cookie_tokenWithParameters:(NSDictionary *)params success:(void(^)(id responseObject))success failure:(void(^)(NSString *error))failure
{
    [LLHttpRequest post:@"index/getToken.do" parameters:params success:^(id responseObject) {
        NSLog(@"%@--->cookie_token",responseObject);
        NSString *token = responseObject[@"resultObject"][@"token"];
        [kUserDefaults setObject:token forKey:@"cookie_token"];
    } failure:^(NSError *error) {
        NSLog(@"%@--->cookie_token失败",error);
    }];
}
//注册
+(void)registWithUserPhone:(NSString *)userPhone userpwd:(NSString *)userpwd mobileCode:(NSString *)mobleCode success:(void(^)(id responseObject))success failure:(void(^)(NSString *error))failure
{
    NSDictionary *paramsDic = @{
                                @"userphone":userPhone,
                                @"userpwd":userpwd,
                                @"usertype":@"0",
                                @"mobileCode":mobleCode,
                                @"token":cookie_token
                                };
    [LLHttpRequest post:@"index/register.do" parameters:paramsDic success:^(id responseObject) {
        NSLog(@"%@--->注册",responseObject);
        success(responseObject);
    } failure:^(NSError *error) {
        NSLog(@"%@--->注册失败",error);
     
    }];


}
//获取验证码
+(void)codeWithUserPhone:(NSString *)userphone success:(void(^)(id responseObject))success failure:(void(^)(NSString *error))failure
{
    NSDictionary *paramsDic = @{
                                @"userphone":userphone,
                                @"token":cookie_token
                                };
    [LLHttpRequest post:@"index/getAppMobileCode.do" parameters:paramsDic success:^(id responseObject) {
        NSLog(@"%@--->验证码",responseObject);
    } failure:^(NSError *error) {
        NSLog(@"%@--->验证码失败",error);
   
    }];
}
//忘记密码
+(void)forgetPasswordWithUserPhone:(NSString *)userphone userpwd:(NSString *)userpwd mobileCode:(NSString *)mobileCode success:(void(^)(id responseObject))success failure:(void(^)(NSString *error))failure
{
    NSDictionary *paramsDic = @{
                                @"userphone":userphone,
                                @"userpwd":userpwd,
                                @"mobileCode":mobileCode,
                                @"token":cookie_token
                                };
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    hub.mode = MBProgressHUDModeText;
    [LLHttpRequest post:@"index/restPwd.do" parameters:paramsDic success:^(id responseObject) {
        NSLog(@"%@--->忘记密码",responseObject);
        hub.labelText = responseObject[@"resultMassage"];
        [hub showAnimated:YES whileExecutingBlock:^{
            sleep(1);
        }completionBlock:^{
            [hub removeFromSuperview];
        }];
        success(responseObject);
    } failure:^(NSError *error) {
        NSLog(@"%@--->忘记密码失败",error);
        hub.labelText = error.localizedDescription;
        [hub showAnimated:YES whileExecutingBlock:^{
            sleep(1);
        }completionBlock:^{
            [hub removeFromSuperview];
        }];
    }];
}
//获取省份
+(void)provinceWithRequestSuccess:(void(^)(id responseObject))success failure:(void(^)(NSString *error))failure
{
    [LLHttpRequest post:@"index/getCountry.do" parameters:nil success:^(id responseObject) {
        
        if (success) {
            success(responseObject);
        }
        
    } failure:^(NSError *error) {
        if (failure) {
            failure(error.localizedDescription);
        }
    }];
}
//获取市
+(void)cityWithProvinceCode:(NSString *)code success:(void(^)(id responseObject))success failure:(void(^)(NSString *error))failure
{
    NSDictionary *paramsDic = @{
                                @"code":code
                                };
    [LLHttpRequest post:@"index/getProvince.do" parameters:paramsDic success:^(id responseObject) {
        if (success) {
            success(responseObject);
        }
        
    } failure:^(NSError *error) {
        if (failure) {
            failure(error.localizedDescription);
        }
    }];

}
//获取区
+(void)districtsWithCityCode:(NSString *)code success:(void(^)(id responseObject))success failure:(void(^)(NSString *error))failure
{
    NSDictionary *paramsDic = @{
                                @"code":code
                                };
    [LLHttpRequest post:@"index/getCity.do" parameters:paramsDic success:^(id responseObject) {
        if (success) {
            success(responseObject);
        }
        
    } failure:^(NSError *error) {
        if (failure) {
            failure(error.localizedDescription);
        }
    }];
}
//获取路线
+(void)getRountiWithDestination:(NSString *)destion withToken:(NSString *)token success:(void(^)(id responseObject))success failure:(void(^)(NSString *error))failure
{
    NSDictionary *paramasDic = @{
                                 @"pageNum" : @"1",
                                 @"startname":destion,
                                 @"token":token
                                 };
    [LLHttpRequest post:@"UserApp/findeRouteList.do" parameters:paramasDic success:^(id responseObject) {
        if (success) {
            success(responseObject);
        }
        
    } failure:^(NSError *error) {
        if (failure) {
            failure(error.localizedDescription);
        }
    }];

}
//创建订单
+(void)createOrderWithOrigin:(NSString *)origin destination:(NSString *)destination orderType:(NSString *)orderType orderUser:(NSString *)orderUser orederNumber:(NSString *)orderNum travelTime:(NSString *)travelTime routeId:(NSString *)routeid andPrice:(NSString *)price success:(void(^)(id responseObject))success failure:(void(^)(NSString *error))failure
{
    NSDictionary *paramsDic = @{
                                @"origin":origin,
                                @"destination":destination,
                                @"orderType":orderType,
                                @"token":orderUser,
                                @"orderNum":orderNum,
                                @"DateTime":travelTime,
                                @"singlemoney":price,
                                @"routeId":routeid
                                };
    [LLHttpRequest post:@"UserApp/createOrder.do" parameters:paramsDic success:^(id responseObject) {
        if (success) {
            success(responseObject);
        }
        
    } failure:^(NSError *error) {
        if (failure) {
            failure(error.localizedDescription);
        }
    }];
}
//订单详情
+(void)orderDetailWithOrderId:(NSString *)orderId success:(void(^)(id responseObject))success failure:(void(^)(NSString *error))failure
{
    NSDictionary *paramsDic = @{
                                @"id":orderId
                                };
    [LLHttpRequest post:@"UserApp/queryOrderById.do" parameters:paramsDic success:^(id responseObject) {
        NSLog(@"%@--->订单详情",responseObject);
        
    } failure:^(NSError *error) {
        NSLog(@"%@--->订单详情失败",error);
    }];
    

}
//订单支付
+(void)payOrderWithOrderId:(NSString *)orderId success:(void(^)(id responseObject))success failure:(void(^)(NSString *error))failure
{
    NSDictionary *paramsDic = @{
                                @"id":orderId,
                                @"paytype":@"APP",
                                @"token":cookie_token
                                };
    [LLHttpRequest post:@"UserApp/createPayOrderInfo.do" parameters:paramsDic success:^(id responseObject) {
     
        
        if (success) {
            success(responseObject);
        }
        
    } failure:^(NSError *error) {
        if (failure) {
            failure(error.localizedDescription);
        }
    }];


}
//订单取消
+(void)cancelOrderWithOrderId:(NSString *)orderId success:(void(^)(id responseObject))success failure:(void(^)(NSString *error))failure
{
    NSDictionary *paramsDic = @{
                                @"id":orderId
                                };
    [LLHttpRequest post:@"UserApp/cancelOrderById.do" parameters:paramsDic success:^(id responseObject) {
        if (success) {
            success(responseObject);
        }
        
    } failure:^(NSError *error) {
        if (failure) {
            failure(error.localizedDescription);
        }
    }];

}
//十五、	查询“我的”订单
+(void)checkOrderWithUserId:(NSString *)userId WithPage:(NSInteger)pagecount success:(void(^)(NSMutableArray * listDatas))success failure:(void(^)(NSString *error))failure
{
    NSDictionary *paramsDic = @{
                                @"orderUser":userId,
                                @"pageNo":[NSString stringWithFormat:@"%ld",pagecount]
                                };
    [LLHttpRequest post:@"UserApp/queryOrderByUser.do" parameters:paramsDic success:^(id responseObject) {
        NSLog(@"%@",responseObject);
        NSDictionary * resultObjectDic = responseObject[@"resultObject"];
        LLOrderRequestModel * requestModel = [[LLOrderRequestModel alloc]initWithDictionary:resultObjectDic error:nil];
        success(requestModel.list);
    } failure:^(NSError *error) {
        NSLog(@"%@--->查询订单失败",error);
    }];

}
//完成订单
+(void)finishTheOrderWith:(NSString *)orderId success:(void(^)(NSMutableArray * listDatas))success failure:(void(^)(NSString *error))failure
{

    NSDictionary *paramsDic = @{
                                @"id":orderId
                                };
    [LLHttpRequest post:@"UserApp/overOrder.do" parameters:paramsDic success:^(id responseObject) {
        NSLog(@"%@",responseObject);
        if (success) {
            success(responseObject);
        }
  
    } failure:^(NSError *error) {
        NSLog(@"%@--->完成订单失败",error);
    }];


}
+(void)commintModificationWithUserName:(NSString *)username WithUserCarNum:(NSString*)userCarNum WithImage:(UIImage *)img
                               success:(void(^)(id responseObject))success failure:(void(^)(NSError *error))failure
{

    NSData *imageData;
    NSString *imageFormat;
    if (UIImagePNGRepresentation(img) != nil) {
        imageFormat = @"Content-Type: image/png \r\n";
        imageData = UIImagePNGRepresentation(img);
        
    }else{
        imageFormat = @"Content-Type: image/jpeg \r\n";
        imageData = UIImageJPEGRepresentation(img, 0.4);
        
    }
    NSDictionary *paramsDic = @{
                                @"username":username,
                                @"usernumber":userCarNum,
                                @"token":cookie_token
                                };
   
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    hub.labelText = MBProgressHUDModeIndeterminate;
    hub.labelText = @"修改中...";
   
    [LLHttpRequest postData:@"usercenter/appUpdateUserInf.do" parameters:paramsDic formData:imageData success:^(id responseObject) {
        NSLog(@"%@",responseObject);
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@", dictionary[@"resultMassage"]);
        if ([dictionary[@"resultMassage"] isEqualToString:@"信息修改成功"]) {
            
            LLUserModel *user = [LLUserHandle getUserData];
            user.UserIconUrl = dictionary[@"resultObject"][@"userheadpath"];
            user.UserName = username;
            user.userid = dictionary[@"resultObject"][@"user"][@"userid"];
            [LLUserHandle saveUserDataWith:user];
            if (success) {
                success(responseObject);
            }
        }
        hub.mode = MBProgressHUDModeText;
        hub.labelText = dictionary[@"resultMassage"];
        [hub showAnimated:YES whileExecutingBlock:^{
            sleep(1);
        }completionBlock:^{
            [hub removeFromSuperview];
        }];
    } failure:^(NSError *error) {
        NSLog(@"%@",error.localizedDescription);
        
        
    }];
}
/**
 *  系统消息列表
 */
+(void)queryAllMessageByUser:(NSString *)userId success:(void(^)(id responseObject))success failure:(void(^)(NSString *error))failure
{
    NSDictionary *paramsDic = @{
                                @"userid":userId
                                };
    [LLHttpRequest post:@"MessageApp/queryAllMessageByUser.do" parameters:paramsDic success:^(id responseObject) {
        NSLog(@"%@",responseObject);
   
        success(responseObject);
    } failure:^(NSError *error) {
        NSLog(@"%@--->查询订单失败",error);
    }];
}

+(void)queryAllOrderByDriver:(NSString *)driverId success:(void(^)(id responseObject))success failure:(void(^)(NSError *error))failure
{
    
    NSDictionary *dic = @{
                          @"driverid":driverId
                          };
    [LLHttpRequest get:@"DriverApp/queryAllOrderByDriver.do" parameters:dic success:^(id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSError *error) {
        
    }];
    
}
+(void)queryOrderDetail:(NSString *)orderId success:(void(^)(id responseObject))success failure:(void(^)(NSError *error))failure
{
    NSDictionary *param = @{
                            @"orderid":orderId
                            };
    [LLHttpRequest get:@"DriverApp/queryOrderDetail.do" parameters:param success:^(id responseObject) {
        
        if (success) {
            success(responseObject);
        }
        
    } failure:^(NSError *error) {
        
    }];
}
+(void)queryUnfinishOrderList:(NSString *)orderId success:(void(^)(id responseObject))success failure:(void(^)(NSError *error))failure
{
    NSDictionary *param = @{
                            @"orderid":orderId
                            };
    [LLHttpRequest get:@"DriverApp/queryUnFinishOrderList.do" parameters:param success:^(id responseObject) {
        
        if (success) {
            success(responseObject);
        }
        
    } failure:^(NSError *error) {
        
    }];
}
@end
